package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.ExpenditureTypeEnum;
import com.ruoyi.system.domain.Expenditure;
import com.ruoyi.system.mapper.ExpenditureMapper;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.IMonth;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.bind.ValidationEvent;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Service
public class MonthImpl implements IMonth {

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Autowired
    private ExpenditureMapper expenditureMapper;

    @Autowired
    private SysUserMapper sysUserMapper;


    @Override
    public int monthFixedExpenditure() {
        //插入成功的支出表
        int index = 0;
        //固定支出包含：房租，人员工资
        //房租
        SysDept dept = new SysDept();
        dept.setRent(new BigDecimal(0));
        List<SysDept> sysDepts = sysDeptMapper.selectDeptList(dept);
        for (SysDept sysDept : sysDepts) {
            Expenditure expenditure = new Expenditure();
            expenditure.setExpenditureType(ExpenditureTypeEnum.RENT.getCode());
            expenditure.setExpenditureTime(new Date());
            expenditure.setExpenditureAmount(sysDept.getRent());
            expenditure.setExpenditureNotes(sysDept.getDeptName() + "，每月固定房租：" + sysDept.getRent() + "元");
            expenditure.setCreateBy("SYSTEM");
            expenditure.setCreateTime(new Date());
            expenditureMapper.insertExpenditure(expenditure);
            index++;
        }

        //人员工资
        SysUser user = new SysUser();
        user.setSalary(new BigDecimal(0));
        List<SysUser> sysUsers = sysUserMapper.selectUserList(user);
        for (SysUser sysUser : sysUsers) {
            Expenditure expenditure = new Expenditure();
            expenditure.setExpenditureType(ExpenditureTypeEnum.SALARY.getCode());
            expenditure.setExpenditureAmount(sysUser.getSalary());
            expenditure.setExpenditureTime(new Date());
            expenditure.setCreateBy("SYSTEM");
            expenditure.setExpenditureNotes(sysUser.getNickName() + ",每月固定工资" + sysUser.getSalary() + "元");
            expenditure.setCreateTime(new Date());
            expenditureMapper.insertExpenditure(expenditure);
            index++;
        }


            return index;

    }

}
